//package com.cxhc.framework.aspectj;
//
//import com.cxhc.common.utils.StringUtils;
//import com.cxhc.framework.config.TransactionConfig;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//import java.lang.annotation.Annotation;
//import java.lang.reflect.Field;
//import java.lang.reflect.InvocationHandler;
//import java.lang.reflect.Method;
//import java.lang.reflect.Proxy;
//
//
///**
// * 多数据源处理
// *
// * @author Dhq
// */
//@Aspect
//@Order(1)
//@Component
//public class TransactionalAspect
//{
//    protected Logger logger = LoggerFactory.getLogger(getClass());
//
//    @Before("@annotation(transactional)")
//    public void before(JoinPoint joinPoint, Transactional transactional) throws Throwable
//    {
//
//        String value = transactional.value();
//        if (StringUtils.isEmpty(value)){
////            Method transactionalMethodFiled = transactional.annotationType().getDeclaredMethod("value");
////            transactionalMethodFiled.setAccessible(true);
////            transactionalMethodFiled.invoke(transactional,TransactionConfig.MASTER_TX);
//
//            //创建注解的代理对象
//            InvocationHandler handler = (proxy, method, args) -> {
//                if ("value".equals(method.getName())){
//                    //返回value值
//                    return TransactionConfig.MASTER_TX;
//                }
//                return method.invoke(transactional,args);
//            };
//            //获取注解的class对象
//            Class<? extends Annotation> aClass = transactional.annotationType();
//            //创建注解的代理实例
//            Transactional transactionalProxy = (Transactional)Proxy.newProxyInstance(aClass.getClassLoader(), new Class[]{aClass}, handler);
//
//            Object target = joinPoint.getTarget();
//            Class<?> targetClass = target.getClass();
//            Annotation[] declaredAnnotations = targetClass.getDeclaredAnnotations();
//            for (int i = 0;i < declaredAnnotations.length;i++) {
//                Annotation declaredAnnotation = declaredAnnotations[i];
//                if (declaredAnnotation.annotationType() == Transactional.class){
//                    declaredAnnotations[i] = transactionalProxy;
//                    break;
//                }
//            }
////            Field annotationsField = Class.class.getDeclaredField("annotations");
////            annotationsField.setAccessible(true);
////            annotationsField.set(targetClass,declaredAnnotations);
//        }
//    }
//
//}
